/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "KeyBoard_Driver.h"
#include "usbd_customhid.h"
#include "usb_device.h"
#include "oled.h"
#include "bmp.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
Lcd_Record Lcd_Value[6];
Lcd_Record Lcd_Value_Pre[6];
uint8_t Key_Board_State = 0;
uint8_t Key_Board_State_Pre = 0;
uint8_t Test_Array[8] = {0x00,0x00,0x1D,0x00,0x00,0x00,0x00,0x00};
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// void Lcd_Display()
// {
//   for(int i = 0;i < 6;i++)
//   {
//     if(Lcd_Value[i].isPressed == True)//如果按键之前被按下
//     {
//       if(LCD_KeyState[Lcd_Value[i].I][Lcd_Value[i].J] == KEYUNPRESSED)//现在没被按下
//       {
//         //消除
//         Lcd_Value[i].isPressed = False;
// //        LCD_ShowString(40,(Lcd_Value[i].Num + 1) * 16,
// //                       (uint8_t *)"     ",WHITE,BLACK,16,0);
//         LCD_Fill(0, (Lcd_Value[i].Num + 1) * 16, LCD_W, (Lcd_Value[i].Num + 2) * 16, BLACK);
//       }
//     }
//   }
//   Press_Num = 0;
//   for (int i = 0; i < 6; i++)
//   {
//     for(int j = 0; j < 5;j++)
//     {
//       if (LCD_KeyState[i][j] == KEYPRESSED)
//       {
//         uint8_t isExist_Flag = 0;
//         for(int index = 0;index < 6;index++)
//         {
//           if (Lcd_Value[index].I == i && Lcd_Value[index].J == j && Lcd_Value[index].isPressed == True)
//           {
//             isExist_Flag = 1;
//             Press_Num++;
//             break;
//           }
//         }
//         if(isExist_Flag == 0)
//         {
//           Lcd_Value[Press_Num].I = i;
//           Lcd_Value[Press_Num].J = j;
//           Lcd_Value[Press_Num].Num = Press_Num;
//           Lcd_Value[Press_Num].isPressed = True;
//           Press_Num++;
//         }
//       }
//     }
//   }
//
//   for (int index = 0; index < Press_Num; index++)
//   {
//     //LCD_Fill(0,(Lcd_Value[index].Num + 1) * 32,LCD_W,(Lcd_Value[index].Num + 2) * 32,BLACK);
//     //LCD_Fill(0, (Lcd_Value[index].Num + 1) * 32, LCD_W, (Lcd_Value[index].Num + 2) * 32, BLACK);
// //    if(Lcd_Value[index].I == 0 && Lcd_Value[index].J == 0)
// //    {
// //      LCD_DrawImage(48, 32, 32,32, Image_Array[0]);
// //    }
// //    else
// //    {
//       LCD_ShowString((128-strlen(myKeyBoard_KeyMap[Lcd_Value[index].I][Lcd_Value[index].J])*8)/2,16 + index * 16,
//                      (uint8_t *)myKeyBoard_KeyMap[Lcd_Value[index].I][Lcd_Value[index].J],WHITE,BLACK,16,0);
// //    }
//   }
// }

void Final_Function()
{
  Switch_Mode();
  myKeyBoard_ScanKeyAndUpdataATBuffer();
  if(g_myKeyBoard_DataWaitForUploadFlag == 1)
  {
    g_myKeyBoard_DataWaitForUploadFlag = 0;
    USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,&BTK05_ATKeyDataPack[0],8);
  }
}



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_SPI2_Init();
  MX_USB_DEVICE_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */

  HAL_TIM_Base_Start(&htim2);
  OLED_Init();
  OLED_Clear();
  OLED_ShowPicture(0,15,128,34,IMAGE,1);
  OLED_Refresh();
  HAL_Delay(2000);
  OLED_Clear();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    Final_Function();
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
